package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.AutoLog;
import com.example.common.Result;
import com.example.domain.entity.Params;
import com.example.domain.entity.Type;
import com.example.service.TypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/type")
public class TypeController {

    private static final Logger log = LoggerFactory.getLogger(TypeController.class);


    @Autowired
    private TypeService typeService;
    
    @GetMapping("/findAll")
    public Result findAll() {
        List<Type> list = typeService.findAll();
        return Result.success(list);
    }

    @PostMapping
    @AutoLog("增加或修改自习室分类信息")
    public Result save(@RequestBody Type type) {
        if (type.getId() == null) {
            typeService.add(type);
        } else {
            typeService.update(type);
        }
        return Result.success();
    }

    @GetMapping("/search")
    public Result findBySearch(Params params) {
        log.info("拦截器已放行，正式调用接口内部，查询自习室分类信息");
        Page<Type> info = typeService.findBySearch(params);
        return Result.success(info);
    }

    @DeleteMapping("/{id}")
    @AutoLog("删除了一条自习室分类信息")
    public Result delete(@PathVariable Integer id) {
        typeService.delete(id);
        return Result.success();
    }
    
}
